import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/Home'
import Login from '@/views/Login'
import Users from '@/components/Users'
import Rights from '@/components/Rights'
import Rules from '@/components/Rules'
import Goods from '@/components/Goods'
import Params from '@/components/Params'
import Cate from '@/components/Cate'
import Dingdan from '@/components/Dingdan'
import Data from '@/components/Data'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    children: [
      {
        path: '/users',
        name: 'users',
        component: Users
      },
      {
        path: '/roles',
        name: 'roles',
        component: Rights
      },
      {
        path: '/rights',
        name: 'rights',
        component: Rules
      },
      {
        path: '/goods',
        name: 'goods',
        component: Goods
      },
      {
        path: '/params',
        name: 'params',
        component: Params
      },
      {
        path: '/categories',
        name: 'categories',
        component: Cate
      },
      {
        path: '/orders',
        name: 'orders',
        component: Dingdan
      },
      {
        path: '/reports',
        name: 'data',
        component: Data
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (token) return next()
  console.log('to : ', to.path)
  console.log('from : ', from.path)
  if (from.path !== 'login') router.push('/login')
})

export default router
